package com.kzj.laimeimei.recyclerCard.cardView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.kzj.laimeimei.R;
import com.kzj.laimeimei.activity.GBaseActivity;
import com.kzj.laimeimei.beans.BookDateTime;
import com.kzj.laimeimei.beans.TimeBean;
import com.kzj.laimeimei.beans.TimeListBean;
import com.kzj.laimeimei.global.App;
import com.kzj.laimeimei.global.Constant;
import com.kzj.laimeimei.recyclerCard.basic.CardItemView;
import com.kzj.laimeimei.recyclerCard.basic.MaterialListAdapter;
import com.kzj.laimeimei.recyclerCard.basic.MaterialListView;
import com.kzj.laimeimei.recyclerCard.card.other.ChooseLeftTimeCard;
import com.kzj.laimeimei.recyclerCard.card.other.ChooseRightTimeCard;
import com.kzj.laimeimei.request.BaseResponse;
import com.kzj.laimeimei.request.ICallBackForRequestServer;
import com.kzj.laimeimei.request.RequestServerManager;

import de.greenrobot.event.EventBus;

/**
 * Created by xyg on 2015/11/9.
 */
public class ChooseTimeLeftCardView extends CardItemView<ChooseLeftTimeCard> {

    private Context context;
    private TextView textView_time;
    /*private TextView textView_title;*/
    MaterialListView materialListViewDate;
    MaterialListView materialListViewTime;
    public ChooseTimeLeftCardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ChooseTimeLeftCardView(Context context) {
        super(context);
        this.context = context;
    }

    public ChooseTimeLeftCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    public void build(final ChooseLeftTimeCard card) {
        super.build(card);
        textView_time = (TextView) findViewById(R.id.textView_time);
        textView_time.setText(card.getTimeBean().getTime());
        materialListViewDate=card.getMaterialListViewDate();
        materialListViewTime=card.getMaterialListViewTime();
        //先判断booktime
        if (card.getBookDateTime()!=null && card.getBookDateTime().getBooking_date().equals(card.getTimeBean().getTime()))
        {
            textView_time.setBackgroundColor(getResources().getColor(R.color.color_f875a3_red));
        }else
        {
            textView_time.setBackgroundColor(getResources().getColor(R.color.white));
        }
       /* if (card.isSelected()) {
            textView_time.setBackgroundColor(getResources().getColor(R.color.color_f875a3_red));
        }else
        {
            textView_time.setBackgroundColor(getResources().getColor(R.color.white));
        }*/
        textView_time.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                BookDateTime bookDateTime =card.getBookDateTime();
                bookDateTime.setBooking_date(textView_time.getText().toString());
                EventBus.getDefault().post(bookDateTime);

                for (int i=0;i<materialListViewDate.getAdapter().getItemCount();i++)
                {
                    ChooseLeftTimeCard chooseLeftTimeCard_temp= (ChooseLeftTimeCard) ((MaterialListAdapter)materialListViewDate.getAdapter()).getCard(i);
                    chooseLeftTimeCard_temp.setIsSelected(false);
                }

                card.setIsSelected(true);
                materialListViewDate.getAdapter().notifyDataSetChanged();

                materialListViewTime.clear();
                TaskGetTimeList(App.getInstance().getLongitude(), App.getInstance().getLatitude(), card.getSid(),App.getInstance().getCity(), card.getTimeBean().getTime());
                /*for (int i = 0; i < 10; i++) {
                    ChooseRightTimeCard chooseRightTimeCard = new ChooseRightTimeCard(context);
                    TimeBean timeBean = new TimeBean();
                    timeBean.setTime(i + "-");
                    chooseRightTimeCard.setTimeBean(timeBean);
                    materialListViewTime.add(chooseRightTimeCard);
                }*/
            }
        });
    }

    private void TaskGetTimeList(String lng,String lat,String sid,String city, final String date) {
        RequestServerManager.getInstance().handleMethod((GBaseActivity) context, materialListViewTime, null, false,
                Constant.getRootUrl() + Constant.Action.Action_Order_Choose_Time, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_Order_Choose_Time,lng,lat,sid,city,date), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {

                        TimeListBean timeListBean = JSON.parseObject(JSON.toJSONString(result),TimeListBean.class);

                        BookDateTime bookDateTime =new BookDateTime();
                        bookDateTime.setBooking_date(date);
                        for (int i = 0; i < timeListBean.getRetval().size(); i++) {
                            if (timeListBean.getRetval().get(i).getIs_use().equals("true"))
                            {
                                bookDateTime.setBooking_time(timeListBean.getRetval().get(i).getTime());
                                break;
                            }
                        }
                        EventBus.getDefault().post(bookDateTime);
                        boolean needChangeTimeStyle=true;
                        for (int i = 0; i < timeListBean.getRetval().size(); i++) {
                            ChooseRightTimeCard chooseRightTimeCard = new ChooseRightTimeCard(context);
                            TimeBean timeBean = new TimeBean();
                            timeBean.setTime(timeListBean.getRetval().get(i).getTime());
                            chooseRightTimeCard.setTimeBean(timeBean);
                            chooseRightTimeCard.setServiceTimeEntity(timeListBean.getRetval().get(i));
                            chooseRightTimeCard.setBookDateTime(bookDateTime);
                            chooseRightTimeCard.setMaterialListView(materialListViewTime);

                            if (timeListBean.getRetval().get(i).getIs_use().equals("true") && needChangeTimeStyle)
                            {
                                chooseRightTimeCard.setIsSelected(true);
                                needChangeTimeStyle=false;
                            }
                            materialListViewTime.add(chooseRightTimeCard);
                        }
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }
}
